package com.ny.zmb.openapi.ysutil.base;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mzlion.easyokhttp.HttpClient;
import com.mzlion.easyokhttp.request.GetRequest;
import com.ny.zmb.openapi.constant.PHPConstant;
import com.ny.zmb.openapi.entity.PmUserToken;
import com.ny.zmb.openapi.service.IPmUserTokenService;
import com.ny.zmb.openapi.util.StringUtil;
import com.ny.zmb.openapi.ysutil.utils.SignUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.PublicKey;
import java.sql.Wrapper;
import java.util.Optional;

@RestController
public class EYinShengBase {
    @Resource
    private IPmUserTokenService tokenService;

    // 发起方商户号
    protected final static String SRC_MERCHANT_NO = "826513173750017";
    // 私钥密码
    protected final static String MERCHANT_PRX_PWD = "15670Xie";
    // 私钥文件 本项目中 readme 文件夹
//    protected final static String MERCHANT_PRX = "sm2/zmbZs.pfx";
//    protected final static String MERCHANT_CER = "sm2/zmbZs.cer";
//    公钥文件
//    protected final static String MERCHANT_PRX = "D:\\WorkSpace\\ZMB\\Java\\zmb\\lib\\zmbZs.pfx";
//    protected final static String MERCHANT_CER = "D:\\WorkSpace\\ZMB\\Java\\zmb\\lib\\zmbZs.cer";
    protected final static String MERCHANT_PRX = System.getProperty("os.name").contains("Linux") ? "sm2/zmbZs.pfx" : "lib/zmbZs.pfx";
    protected final static String MERCHANT_CER = System.getProperty("os.name").contains("Linux") ? "sm2/zmbZs.cer" : "lib/zmbZs.cer";

    // 公钥密码
    protected static String MERCHANT_PASSWORD = "";

    /**
     * 获取公钥
     *
     * @return
     */
    public static synchronized String getPublicKey() {
        if (StringUtil.isBlank(MERCHANT_CER)) {
            try {
                MERCHANT_PASSWORD = SignUtils.getCerInfo(MERCHANT_CER);
            } catch (Exception var) {
                throw new RuntimeException(var.getMessage());
            }
        }
        return MERCHANT_PASSWORD;
    }

    public static void main(String[] args) throws Exception {
        InputStream fileInputStream = new FileInputStream(MERCHANT_CER);
        PublicKey publicKeyFromCert = SignUtils.getPublicKeyFromCert(fileInputStream, "1111");
//        System.out.println(SignUtils.getCerInfo(MERCHANT_CER));
        System.out.println(new String(publicKeyFromCert.getEncoded()));
    }


    /**
     * 获取token信息
     *
     * @return
     */
    private static String getToken() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String headerValue = request.getHeader("token");
        return headerValue;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public PmUserToken getUserToken() {
        String path = "/api/user/info";
        String userInfo = getToken();
        if (StringUtil.isBlank(userInfo)) {
            throw new RuntimeException("登录信息过期");
        }
        GetRequest header = HttpClient.get(PHPConstant.BASE_URL + path).header("token", userInfo);
        String s = null;
        try {
            s = header.execute().asString();
        } catch (Exception var) {
            throw new RuntimeException("登录信息已过期");
        }
        JSONObject jsonObject = JSONObject.parseObject(s);
        if (1 == jsonObject.getInteger("code")) {
            JSONObject data = jsonObject.getJSONObject("data");
            return PmUserToken.builder()
                    .userId(data.getInteger("id"))
                    .build();
        }
        throw new RuntimeException("登录信息已过期");
    }
}
